package condominio.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import condominio.excepcion.DAOExcepcion;
import condominio.modelo.Junta;
import condominio.modelo.Tema;
import condominio.modelo.Usuario;
import condominio.util.ConexionBD;

public class JuntaDAO extends BaseDAO{

	public Junta insertar(Junta vo) throws DAOExcepcion {

		System.out.println("JuntaDAO: insertar()");
		String query = "INSERT INTO Junta (idjunta,nombre,fecha,acuerdo,hora) VALUES (?,?,?,?,?)";
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;

		try {
			con = ConexionBD.obtenerConexion();
			con.setAutoCommit(false);
			stmt = con.prepareStatement(query);
			stmt.setInt(1, vo.getIdjunta()); 
			stmt.setString(2, vo.getNombre());
			stmt.setString(3,vo.getFecha());                      
			stmt.setString(4, vo.getAcuerdo());
			stmt.setString(5, vo.getHora());

			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo insertar");
			}

			int idp = 0;
			query = "SELECT LAST_INSERT_ID()";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			if (rs.next()) {
				idp = rs.getInt(1);
			}
			vo.setIdJunta(idp);
			for (Usuario ped : vo.getDirigentes()) {
				query = "INSERT INTO JuntaXPersona(idUsuario, idjunta) VALUES (?,?)";
				stmt = con.prepareStatement(query);
				stmt.setInt(1, ped.getIdUsuario());
				stmt.setInt(2, idp);

				int u = stmt.executeUpdate();
				if (u != 1) {
					throw new SQLException("No se pudo insertar");
				}
				int idd = 0;
				query = "SELECT LAST_INSERT_ID()";
				stmt = con.prepareStatement(query);
				rs = stmt.executeQuery();
				if (rs.next()) {
					idd = rs.getInt(1);
				}
				ped.setIdUsuario(idd);
			}
			
			
			int idp2 = 0;
			query = "SELECT LAST_INSERT_ID()";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			if (rs.next()) {
				idp2 = rs.getInt(1);
			}
			vo.setIdJunta(idp2);
			for (Tema ped : vo.getTemas()) {
				query = "INSERT INTO DetalleTemaJunta(idDetalleTemaJunta, idjunta, tema) VALUES (?,?,?)";
				stmt = con.prepareStatement(query);
				stmt.setInt(1, ped.getIdDetalleTemaJunta());
				stmt.setInt(2, idp2);
				stmt.setString(3, ped.getTema());

				int u = stmt.executeUpdate();
				if (u != 1) {
					throw new SQLException("No se pudo insertar");
				}
				int idd = 0;
				query = "SELECT LAST_INSERT_ID()";
				stmt = con.prepareStatement(query);
				rs = stmt.executeQuery();
				if (rs.next()) {
					idd = rs.getInt(1);
				}
				ped.setIdDetalleTemaJunta(idd);
			}
			
			
			
			
			con.commit();
		} catch (SQLException e) {
			try {
				con.rollback();
			} catch (SQLException e1) {
				throw new DAOExcepcion(e.getMessage());
			}
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			try {
				con.setAutoCommit(true);
			} catch (SQLException e) {
				throw new DAOExcepcion(e.getMessage());
			}
			
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}
}
